package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.OrgResultService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 联盟机构研究成果管理
 */
@RestController
@RequestMapping("/bk")
public class OrgResultController {

    private static final Logger logger = LogManager.getLogger(OrgResultController.class);
    
    @Resource
    private OrgResultService orgResultService;

    @LoginRequired
    @GetMapping("/clickOrgResult")
    @ApiOperation(value = "点击研究成果事件")
    public JsonResult clickOrgResult(
            @ApiParam(value = "研究成果ID", required = true) @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            orgResultService.clickOrgResult(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgResult")
    @ApiOperation(value = "新增研究成果")
    public JsonResult addOrgResult(
            @ApiParam(value = "研究成果标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "研究成果副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);

            orgResultService.addOrgResult(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgResult")
    @ApiOperation(value = "更新研究成果")
    public JsonResult updateOrgResult(
            @ApiParam(value = "研究成果ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "研究成果标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "研究成果副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);
            orgResultService.updateOrgResult(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgResult")
    @ApiOperation(value = "删除研究成果")
    public JsonResult deleteOrgResult(
            @ApiParam(value = "研究成果ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            orgResultService.deleteOrgResult(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrgResult")
    @ApiOperation(value = "禁用或启用研究成果")
    public JsonResult disableOrEnableOrgResult(
            @ApiParam(value = "研究成果ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgResultService.disableOrEnableOrgResult(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgResult")
    @ApiOperation(value = "获取研究成果列表")
    public JsonResult listOrgResult(
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("typeId", typeId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgResultService.listOrgResult(pageData));
            result.setResultNum(orgResultService.listOrgResultCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgResultType")
    @ApiOperation(value = "新增研究成果类型")
    public JsonResult addOrgResultType(
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "研究成果类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "研究成果类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("names", names);
            pageData.put("remark", remark);

            orgResultService.addOrgResultType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgResultType")
    @ApiOperation(value = "更新研究成果类型")
    public JsonResult updateOrgResultType(
            @ApiParam(value = "研究成果类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "研究成果类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "研究成果类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("orgId", orgId);
            pageData.put("remark", remark);

            orgResultService.updateOrgResultType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgResultType")
    @ApiOperation(value = "删除研究成果类型")
    public JsonResult deleteOrgResultType(
            @ApiParam(value = "研究成果类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            orgResultService.deleteOrgResultType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableOrgResultType")
    @ApiOperation(value = "禁用或启用研究成果")
    public JsonResult disableOrEnableOrgResultType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgResultService.disableOrEnableOrgResultType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgResultType")
    @ApiOperation(value = "获取研究成果类型列表")
    public JsonResult listOrgResultType(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgResultService.listOrgResultType(pageData));
            result.setResultNum(orgResultService.listOrgResultTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listOrgResultTypeAll")
    @ApiOperation(value = "获取研究成果类型列表所有")
    public JsonResult listOrgResultTypeAll(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);

            result.setResult(orgResultService.listOrgResultTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
